<?php
include($_SERVER['DOCUMENT_ROOT'].'/inc/Function.php');
include($_SERVER['DOCUMENT_ROOT'].'/inc/PHPExcel-1.8/Classes/PHPExcel.php');

if(isset($_GET['act'])){
	$act = $_GET['act'];
	switch($act){
		case "user":
		userlistdc();
		break;
		case "goodsdc":
		goodsdc();
		break;
		case "tixian":
		txlistdc();
		break;
		case "orderlist":
		fhlistdc();
		break;	
		case "spend":
		cwlistdc();
		break;
		case "cz":
		czlistdc();
		break;		
		case "cashlist":
		cashlistdc();
		break;
		case "month":
		monthdc();
		break;
		case "monthlist":
		monthlistdc();
		break;		
		default:
		echo "ERROR";
		break;
	}
}else{
	http_response_code(404);
}

function excel($title,$head,$data){	
	$ex = new PHPExcel;
	$addexcel = PHPExcel_IOFactory::createWriter($ex,'Excel5');
	$sheet = $ex->getActiveSheet(0);	
	$sheet->setTitle($title);
	$excelarr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ");
	for($i=0;$i<count($head);$i++){
		$sheet->setCellValue($excelarr[$i]."1",$head[$i]);
	}
	for($i=0;$i<count($data);$i++){
		for($n=0;$n<count($data[$i]);$n++){
		    $sheet->setCellValueExplicit($excelarr[$n]."".($i+2),$data[$i][$n], PHPExcel_Cell_DataType::TYPE_STRING);
		}		
	}
	header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header("Content-Disposition:attachment;filename=$title.xlsx");
	header('Cache-Control:max-age=0');
	$addexcel->save('php://output');
}

function goodsdc(){
	global $conf;
	global $catarr;
	$db = new db($conf);	
	$where = "Id>0";
	$list = $db->field(array("*"))->where($where)->order("Id desc")->select("sc_goods");
	$head = array("ID","所属专区","所属分类","功能","材质","商品名称","市场价","会员价","置换费","库存","销量","排序");										
	$n = 0;
	for($i=0;$i<count($list);$i++){	
		$list[$i]['name'] = '';
		$list[$i]['tagname1'] = '';
		$list[$i]['tagname2'] = '';
		$info = $db->field(array("name"))->where("Id=".$list[$i]['cid'])->select("sc_catogry");
		if($info){			
			$list[$i]['name'] = $info[0]['name'];							
		}
		if($list[$i]['tag1']>0){
			$info = $db->field(array("name"))->where("Id=".$list[$i]['tag1'])->select("sc_tag");
			if($info){			
				$list[$i]['tagname1'] = $info[0]['name'];							
			}
		}
		if($list[$i]['tag2']>0){
			$info = $db->field(array("name"))->where("Id=".$list[$i]['tag2'])->select("sc_tag");
			if($info){			
				$list[$i]['tagname2'] = $info[0]['name'];							
			}
		}	
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$catarr[$list[$i]['cplx']];
		$arr[$n][]=$list[$i]['name'];
		$arr[$n][]=$list[$i]['tagname1'];
		$arr[$n][]=$list[$i]['tagname2'];
		$arr[$n][]=$list[$i]['gname'];
		$arr[$n][]=$list[$i]['yprice'];
		$arr[$n][]=$list[$i]['cash'];
		$arr[$n][]=$list[$i]['kuaidif'];
		$arr[$n][]=$list[$i]['gtotal'];				
		$arr[$n][]=$list[$i]['selltotal'];				
		$arr[$n][]=$list[$i]['px'];				
		$n++;
	}
	if($n>0){
		excel("商品记录",$head,$arr);
	}else{
		echo "<script>window.location.href='goodslist.php';</script>";
	}	
}

function cashlistdc(){
	global $conf;
	global $jjarr;
	$db = new db($conf);	
	$where = "t.Id>0";
	$uname = isset($_GET['uname'])?trim($_GET['uname']):"";
	$stime = isset($_GET['stime'])?trim($_GET['stime']):"";
	$etime = isset($_GET['etime'])?trim($_GET['etime']):"";
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and t.uid=".$info[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(t.addtime)>= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(t.addtime)<= to_days('".$etime."')";
	}
	$list = $db->field(array("t.*","m.tname","m.uname"))->where($where)->order("t.Id desc")->select("sc_cash as t left join sc_member as m on m.Id=t.uid");
	$head = array("ID","账号","姓名","备注","收益","时间");										
	$n = 0;
	for($i=0;$i<count($list);$i++){		
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$list[$i]['uname'];
		$arr[$n][]=$list[$i]['tname'];
		if($list[$i]['content']){
			$arr[$n][]=$list[$i]['content'];
		}else{
			$arr[$n][]=$jjarr[$list[$i]['type']];
		}
		if($list[$i]['adda']==1){
			$arr[$n][]="+".$list[$i]['cash'];
		}else{
			$arr[$n][]="-".$list[$i]['cash'];
		}
		
		$arr[$n][]=$list[$i]['addtime'];				
		$n++;
	}
	if($n>0){
		excel("奖励记录",$head,$arr);
	}else{
		echo "<script>window.location.href='cashlist.php';</script>";
	}	
}


function cwlistdc(){
	global $conf;
	global $payarr;
	$db = new db($conf);	
	$where = "t.type>=0";
	$uname = isset($_GET['uname'])?trim($_GET['uname']):"";
	$stime = isset($_GET['stime'])?trim($_GET['stime']):"";
	$etime = isset($_GET['etime'])?trim($_GET['etime']):"";
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and t.uid=".$info[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(t.addtime)>= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(t.addtime)<= to_days('".$etime."')";
	}
	$list = $db->field(array("t.*","m.tname","m.uname"))->where($where)->order("t.Id desc")->select("sc_spend as t left join sc_member as m on m.Id=t.uid");
	$head = array("ID","会员账号","消费明细","消费金额","支付方式","消费时间");										
	$n = 0;
	for($i=0;$i<count($list);$i++){		
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$list[$i]['uname']."(".$list[$i]['tname'].")";
		$arr[$n][]=$list[$i]['content'];
		$arr[$n][]=$list[$i]['cash'];
		$arr[$n][]=$payarr[$list[$i]['pay']];		
		$arr[$n][]=$list[$i]['addtime'];				
		$n++;
	}
	if($n>0){
		excel("消费记录",$head,$arr);
	}else{
		echo "<script>window.location.href='record_ye_list.php';</script>";
	}	
}

function fhlistdc(){
	global $conf;
	global $osarr1;
	global $osarr2;
	global $payarr;
	$db = new db($conf);	
	$uname = isset($_GET['uname'])?trim($_GET['uname']):"";
	$tname = isset($_GET['tname'])?trim($_GET['tname']):"";
	$time = isset($_GET['time'])?trim($_GET['time']):"";
	$state = isset($_GET['state'])?trim($_GET['state']):5;
	$cplx = isset($_GET['cplx'])?intval($_GET['cplx']):1;
	$ocode = isset($_GET['ocode'])?intval($_GET['ocode']):"";
	$where = " o.state<5 and  o.state>0 ";	
	if($state<5){
		$where = $where." and o.state=$state";
	}	
	if($tname){
		$where = $where." and o.tname='".$tname."'";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and o.uid=".$info[0]['Id'];
		}else{
			$where = $where." and o.uid=0";
		}	
	}
	if($time){
		$time = $time." 00:00:00";
		$where = $where." and to_days(o.addtime)= to_days('".$time."')";
	}	
	if($cplx){
		$where = $where." and o.cplx='".$cplx."'";
	}
	if($ocode){
		$where = $where." and o.ocode='".$ocode."'";
	}
	$list = $db->field(array("o.*","m.uname","m.tname as mtname","m.phone as mphone"))->where($where)->order("o.Id desc")->select("sc_orders as o left join sc_member as m on m.Id=o.uid");	
	$head = array("ID","账号","姓名","电话","备注","金额","收货地址","支付方式","状态","下单时间","完成时间","快递公司","快递单号");		
	$n = 0;
	for($i=0;$i<count($list);$i++){	
		if(!$list[$i]['tname']){
			$list[$i]['tname'] = $list[$i]['mtname'];
			$list[$i]['phone'] = $list[$i]['mphone'];
		}
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$list[$i]['uname'];
		$arr[$n][]=$list[$i]['tname'];
		$arr[$n][]=$list[$i]['phone'];
		$arr[$n][]=$list[$i]['content'];		
		$arr[$n][]=$list[$i]['cash'];
		$arr[$n][]=$list[$i]['address'];
				
		$arr[$n][]=$payarr[$list[$i]['pay']];
		if($list[$i]['type']==1){
			$arr[$n][]=$osarr1[$list[$i]['state']];
		}else{
			$arr[$n][]=$osarr2[$list[$i]['state']];			
		}
		$arr[$n][]=$list[$i]['addtime'];		
		$arr[$n][]=$list[$i]['endtime'];			
		$arr[$n][]=$list[$i]['trackname'];			
		$arr[$n][]=$list[$i]['trackcode'];			
		$n++;
	}
	if($n>0){
		excel("订单记录",$head,$arr);
	}else{
		echo "<script>window.location.href='orderlist.php';</script>";
	}	
}
function txlistdc(){
	global $conf;
	global $txarr;
	$db = new db($conf);	
	$where = "t.state=0";
	$uname = isset($_GET['uname'])?trim($_GET['uname']):"";
	$stime = isset($_GET['stime'])?trim($_GET['stime']):"";
	$etime = isset($_GET['etime'])?trim($_GET['etime']):"";
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and t.uid=".$info[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(t.addtime)>= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(t.addtime)<= to_days('".$etime."')";
	}
	$list = $db->field(array("t.*","m.tname","m.uname"))->where($where)->order("t.Id desc")->select("sc_tixian as t left join sc_member as m on m.Id=t.uid");
	$head = array("ID","账户","姓名","提现类型","提现金额","实付金额","联系方式","银行名称","银行卡号","持卡人","申请时间","审核时间","申请状态");										
	$n = 0;
	for($i=0;$i<count($list);$i++){		
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$list[$i]['uname'];
		$arr[$n][]=$list[$i]['tname'];
		if($list[$i]['type']==1){
			$arr[$n][]="收益";
		}else{
			$arr[$n][]="购物值";
		}
		
		$arr[$n][]=$list[$i]['num'];
		$arr[$n][]=$list[$i]['cash'];
		$arr[$n][]=$list[$i]['phone'];		
		$arr[$n][]=$list[$i]['bname'];
		$arr[$n][]=$list[$i]['bcard'];
		$arr[$n][]=$list[$i]['btname'];			
		$arr[$n][]=$list[$i]['addtime'];	
		$arr[$n][]=$list[$i]['passtime'];		
		$arr[$n][]=$txarr[$list[$i]['state']];			
		$n++;
	}
	if($n>0){
		excel("提现记录",$head,$arr);
	}else{
		echo "<script>window.location.href='tixianlist.php';</script>";
	}	
}

function userlistdc(){
	global $conf;
	global $uarr1;
	global $tdarr;
	global $tdarr1;
	$db = new db($conf);
	$uname = isset($_GET['uname'])?$_GET['uname']:"";
	$level = isset($_GET['level'])?$_GET['level']:-1;
	$tlevel = isset($_GET['tlevel'])?$_GET['tlevel']:0;
	$where = "state >= 0 ";
	if($uname){
		$where = $where." and (uname='".$uname."' or tname='".$uname."' or phone='".$uname."')";
	}
	if($level>-1){		
		$where = $where." and level=$level";		
	}
	if($tlevel>0){		
		$where = $where." and tlevel=$tlevel";		
	}
	$list = $db->field(array("*"))->where($where)->order("Id asc")->select("sc_member");
	$head = array("ID","账号","姓名","手机号","链动级别","团队级别","余额","收益","直推","团队人数","个人业绩","团队业绩","推荐人","老板","注册时间","状态");															
	$n = 0;
	for($i=0;$i<count($list);$i++){		
		$list[$i]['tjname'] = "";		
		if($list[$i]['tjid']>0){
			$info = $db->field(array("*"))->where("Id=".$list[$i]['tjid'])->select("sc_member");
			if($info){
				$list[$i]['tjname'] = $info[0]['uname']."[".$info[0]['tname']."]";
			}
		}
		$list[$i]['lbname'] = "";		
		if($list[$i]['lbid']>0){
			$info = $db->field(array("*"))->where("Id=".$list[$i]['lbid'])->select("sc_member");
			if($info){
				$list[$i]['lbname'] = $info[0]['uname']."[".$info[0]['tname']."]";
			}
		}		
		$ztnum = $db->field(['count(*)'])->where('tjid='.$list[$i]['Id'])->select("sc_member");
		$list[$i]['ztnum'] = $ztnum[0]['count(*)']>0?$ztnum[0]['count(*)']:0;
		
		$arr[$n][]=$list[$i]['Id'];
		$arr[$n][]=$list[$i]['uname'];
		$arr[$n][]=$list[$i]['tname'];		
		$arr[$n][]=$list[$i]['phone'];		
		$arr[$n][]=$tdarr[$list[$i]['level']];		
		$arr[$n][]=$tdarr1[$list[$i]['tlevel']];		
		$arr[$n][]=$list[$i]['cash'];			
		$arr[$n][]=$list[$i]['jjcash'];	
		$arr[$n][]=$list[$i]['ztnum'];
		$arr[$n][]=$list[$i]['teamnum'];		
		$arr[$n][]=$list[$i]['selforder'];		
		$arr[$n][]=$list[$i]['teamorder'];		
		$arr[$n][]=$list[$i]['tjname'];
		$arr[$n][]=$list[$i]['lbname'];
		$arr[$n][]=$list[$i]['addtime'];
		$arr[$n][]=$uarr1[$list[$i]['state']];			
		$n++;
	}
	if($n>0){
		excel("会员记录",$head,$arr);
	}else{
		echo "<script>window.location.href='userlist.php';</script>";
	}	
}

?>